package com.yty.system.controller;


import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理api")
public class FileController {


    @GetMapping("exportPDF")
    @ApiOperation(value = "文件导出到PDF",notes = "文件导出到PDF")
    public void exportPDF(HttpServletResponse response) throws Exception{
        try {
            exportWord(response, "2012001.ftl", "/templates/ftl/");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportWord(HttpServletResponse response, String templateName, String templatePath) throws Exception{
        Configuration configuration = new Configuration(Configuration.getVersion()); // 创建一个Configuration对象

        configuration.setClassForTemplateLoading(this.getClass(), templatePath);

        configuration.setDefaultEncoding("utf-8");

        //必须加此参数，否则任意key的值为空freemark都会报错
        configuration.setClassicCompatible(true);
        // 选择模板
        Template template = configuration.getTemplate(templateName); //加载模板

        // 导出文件名
        String fileName = System.currentTimeMillis() + ".doc";
        // 导出文件路径
        String path = "D:\\system\\ee\\模板\\" + fileName;
        // 创建文件
        File file = new File(path);

        Writer out = new FileWriter(file);
        // 填充数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("surveyPersonName", "孙悟空");
        jsonObject.put("createTime", "2012-09");

        //调用模板对象的process方法输出文件
        template.process(jsonObject, out);

        // 下载文件
        downloadFile(response, file, out);

        ITextRenderer renderer = new ITextRenderer();
        // 把html代码传入渲染器中
        // 设置模板中的图片路径 （这里的images在resources目录下） 模板中img标签src路径需要相对路径加图片名 如<img src="images/xh.jpg"/>
        // String url = ImportExcelUtil.class.getClassLoader().getResource("images").toURI().toString();
        // renderer.getSharedContext().setBaseURL(url);
        renderer.layout();

        renderer.finishPDF();
    }



    public void downloadFile(HttpServletResponse response, File file, Writer out) throws Exception{
        // 下载文件
        byte[] buffer = new byte[1024];
        response.addHeader("Content-Disposition","attachment;filename=" + new String(file.getName().getBytes(), "ISO-8859-1"));
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = response.getOutputStream();
        int i = bis.read(buffer);
        while (i != -1) {
            os.write(buffer, 0, i);
            i = bis.read(buffer);
        }
        // 关闭流
        os.close();
        bis.close();
        fis.close();
        out.close();
        file.delete();
    }
}






















